//
//  OrderFooterDownView.m
//  FD
//
//  Created by xiaofang wang on 2019/1/8.
//  Copyright © 2019年 ZhiYuan Network. All rights reserved.
//

//  0，支付，4评价，其他都是再来一单

#define kLeftRightMargin FDScaleWidth(60)

#import "OrderFooterDownView.h"
@interface OrderFooterDownView()
//下===========
/**灰色背景view*/
@property (nonatomic,  strong) UIView *bottomView;
/**leftBtn*/
@property (nonatomic,  strong) UIButton *leftBtn;
/**右边按钮*/
@property (nonatomic,  strong) UIButton *rightBtn;

@end

@implementation OrderFooterDownView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.bottomView];
    self.backgroundColor = kMainLightGrayColor;
    [self.bottomView addSubview:self.leftBtn];
//    [self.leftBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//    [self.rightBtn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];

    [self lightGrayButton:self.leftBtn];
    [self lightRedButton:self.rightBtn];
    [self.bottomView addSubview:self.rightBtn];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    //按钮
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(100, 30));
        make.height.mas_equalTo(26);
        make.width.mas_greaterThanOrEqualTo(80);
        make.centerY.equalTo(self.bottomView.mas_centerY);
        make.left.equalTo(self.bottomView.mas_left).offset(kLeftRightMargin);
    }];
    
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(100, 30));
        make.height.mas_equalTo(26);
        make.width.mas_greaterThanOrEqualTo(80); //国际化可能会出问题
        make.centerY.equalTo(self.bottomView.mas_centerY);
        make.right.equalTo(self.bottomView.mas_right).offset(-kLeftRightMargin);
    }];
}

- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [UIView new];
    }
    return _bottomView;
}


//getter
- (UIButton *)leftBtn {
    if (!_leftBtn) {
        
        _leftBtn =  [self createBtnWithTitle:NSLocalizedString(LianXiShangJia, @"联系商家") action:@selector(clickLeftBtnAction:)];
    }
    return _leftBtn;
}
- (UIButton *)rightBtn {
    if (!_rightBtn) {
        NSString *zlydStr = NSLocalizedString(Order_OrderAgain, @"再来一单");
        _rightBtn = [self createBtnWithTitle:zlydStr action:@selector(clickRightBtnAction:)];
    }
    return _rightBtn;
}

- (UIButton *)createBtnWithTitle:(NSString *)title action:(SEL)action {
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setTitle:title forState:(UIControlStateNormal)];
    //切边框
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 5;
    btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    btn.layer.borderWidth= 1.0;
    //事件
    [btn addTarget:self action:action forControlEvents:(UIControlEventTouchUpInside)];
    
    //15号字体
    btn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    
    return btn;
}

/**更新按钮文字状态 */
- (void)lightGrayButton:(UIButton *)button {
    
    //灰色：999999
    UIColor *hui = [UIColor rb_colorHex:0x999999];
    [button setTitleColor:hui forState:(UIControlStateNormal)];
    button.layer.borderColor = hui.CGColor;
}

- (void)lightRedButton:(UIButton *)button {
    //红色：e54b4b
    UIColor *r = [UIColor rb_colorHex:0xe54b4b];
    [button setTitleColor:r forState:(UIControlStateNormal)];
    button.layer.borderColor = r.CGColor;
}




- (void)clickLeftBtnAction:(UIButton *)btn {
    
    FDLog(@"点击左 按钮 %s---%d",__FUNCTION__,__LINE__);
    
    if (self.clickLeftBtnBlock) {
        self.clickLeftBtnBlock();
    }
}

//再来一单
- (void)clickRightBtnAction:(UIButton *)btn {
    
    if (self.clickRightBtnBlock) {
        self.clickRightBtnBlock();
    }
}


#pragma mark - 接口
/**
 1-23 更新底部按钮文字&状态。
 
 @param status 订单状态
 @param paytype 支付方式 0:线上支付 1:线下支付
 @param deliveryType 订单配送方式 0:外送 1:自提 2:门店预约
 */
- (void)orderFooterDownViewUpdateRightBtnTitleWithStatus:(NSNumber *)status payType:(NSNumber *)paytype deliveryType:(NSNumber *)deliveryType {
    
    //在线支付显示逻辑 payementType:0 status:0 显示
    //payementType:1 status:0,1,2,3 显示
    
    //再来一单显示逻辑 status:5,6,7,8,9 显示
    
    //评价显示逻辑 status:4
    NSString *ljzfStr = NSLocalizedString(Order_PayNow, @"立即支付");
    NSString *zlydStr = NSLocalizedString(Order_OrderAgain, @"再来一单");
    if ([paytype isEqualToNumber:@(0)] &&
        [status isEqualToNumber:@(0)]) {
        [self.rightBtn setTitle:ljzfStr forState:(UIControlStateNormal)];
        [self setLightRedButton:self.rightBtn];
        
    }else if ([paytype isEqualToNumber:@(1)]) {
        if ([status isEqualToNumber:@(0)] ||
            [status isEqualToNumber:@(1)] ||
            [status isEqualToNumber:@(2)] ||
            [status isEqualToNumber:@(3)]) {
            [self.rightBtn setTitle:ljzfStr forState:(UIControlStateNormal)];
            [self setLightRedButton:self.rightBtn];
        }else if ([status isEqualToNumber:@(4)]) { //2019-2-18
            [self.rightBtn setTitle:NSLocalizedString(L_Order_Evaluate, @"评价") forState:(UIControlStateNormal)];
            [self setLightRedButton:self.rightBtn];
        }
        
    }else if ([status isEqualToNumber:@(4)]) {
        [self.rightBtn setTitle:NSLocalizedString(L_Order_Evaluate, @"评价") forState:(UIControlStateNormal)];
        [self setLightRedButton:self.rightBtn];
    }else if([status isEqualToNumber:@(5)]||
             [status isEqualToNumber:@(6)]||
             [status isEqualToNumber:@(7)]||
             [status isEqualToNumber:@(8)]||
             [status isEqualToNumber:@(9)]){
        [self.rightBtn setTitle:zlydStr forState:(UIControlStateNormal)];
        
    }else if([paytype isEqualToNumber:@(0)] &&
             [status isEqualToNumber:@(3)]) { //再来一单 &&
        [self.rightBtn setTitle:zlydStr forState:(UIControlStateNormal)];
    }else {
        FDLog(@"其他状态不处理");
    }
}



//!!!!:更新按钮文字、颜色
/**更新按钮文字状态 */
- (void)setLightGrayButton:(UIButton *)button {
    
    //灰色：999999
    UIColor *hui = [UIColor rb_colorHex:0x999999];
    [button setTitleColor:hui forState:(UIControlStateNormal)];
    button.layer.borderColor = hui.CGColor;
}

- (void)setLightRedButton:(UIButton *)button {
    //红色：e54b4b
    UIColor *r = [UIColor rb_colorHex:0xe54b4b];
    [button setTitleColor:r forState:(UIControlStateNormal)];
    button.layer.borderColor = r.CGColor;
}
    
//- (void)updateRightBtnTitleWithStatus:(NSNumber *)status payType:(NSNumber *)paytype{
//    
//    if ([status isEqualToNumber:@(0)]) {
//        if ([paytype isEqualToNumber:@(1)]) { //线下支付
//            [self.rightBtn setTitle:@"再来一单" forState:(UIControlStateNormal)];
//        }else {
//            [self.rightBtn setTitle:@"立即支付" forState:(UIControlStateNormal)];
//        }
//    }else if([status isEqualToNumber:@(4)]) {
//        [self.rightBtn setTitle:@"评价" forState:(UIControlStateNormal)];
//    }else {
//        [self.rightBtn setTitle:@"再来一单" forState:(UIControlStateNormal)];
//    }
//}
@end
